<template>
  <!--流程业务表单-->
  <div class="form-main">
    <a-card :bordered="false" style="padding: 0px">
      <a-form @submit="handleSubmit" :form="form" style="padding: 0px">
        <a-form-item label="标题" v-if="!disabled"
           :labelCol="{lg: {span: 7}, sm: {span: 7} }"
           :wrapperCol="{lg: {span: 10}, sm: {span: 17} }">
          <a-input :disabled="disabled"
            v-model="title"
            v-decorator="['title',{rules: [{ required: true, message: '请输入标题' }]}]"
            title="title"
            placeholder="给目标起个名字" />
        </a-form-item>
        <a-form-item v-if="!disabled" :wrapperCol="{ span: 24 }" style="text-align: center" >
          <a-button type="primary" :disabled="disabled || btndisabled" @click="handleSubmit">保存</a-button>
          <a-button style="margin-left: 8px" :disabled="disabled" @click="close">取消</a-button>
        </a-form-item>
      </a-form>
      <div v-if="task" style="text-align: center">
        <purchase-order ref="purchaseOrder"></purchase-order>
        <purchase-order-stock-in ref="purchaseOrderStockIn"></purchase-order-stock-in>
        <purchase-order-sales-return ref="purchaseOrderSalesReturn"></purchase-order-sales-return>
        <in-or-out-order ref="inOrOutOrder"></in-or-out-order>
        <customer-contract ref="customerContract"></customer-contract>
        <transfers-order ref="transfersOrder"></transfers-order>
        <project-settlement ref="projectSettlement"></project-settlement>
        <project-payment ref="projectPayment"></project-payment>
        <customer-contract ref="customerContract"></customer-contract>
        <customer-scrap-order ref="customerScrapOrder"></customer-scrap-order>
        <a-button type="primary" v-if="processData.status === 1"  @click="passTask">通过</a-button>
        <a-button style="margin-left: 8px" v-if="processData.status === 1"  @click="backTask">驳回</a-button>
      </div>
    </a-card>
  </div>
</template>

<script>
  import PurchaseOrder from './PurchaseOrder';
  import PurchaseOrderStockIn from './PurchaseOrderStockIn';
  import PurchaseOrderSalesReturn from './PurchaseOrderSalesReturn';
  import InOrOutOrder from './InOrOutOrder';
  import TransfersOrder from './TransfersOrder';
  import ProjectSettlement from './ProjectSettlement';
  import ProjectPayment from './ProjectPayment'
  import CustomerContract from './CustomerContract';
  import CustomerScrapOrder from './CustomerScrapOrder'

  import pick from "lodash.pick";
  import { getAction, postAction } from '@/api/manage';
  export default {
    name: 'demoForm',
    components: {
      PurchaseOrder,
      PurchaseOrderStockIn,
      PurchaseOrderSalesReturn,
      InOrOutOrder,
      TransfersOrder,
      ProjectSettlement,
      ProjectPayment,
      CustomerContract,
      CustomerScrapOrder,
    },
    props:{
      /*全局禁用，可表示查看*/
      disabled:{
        type:Boolean,
        default:false,
        required:false
      },
      /*流程数据*/
      processData:{
        type:Object,
        default:()=>{return {}},
        required:false
      },
      purchaseOrderId: '',
      /*是否新增*/
      isNew: {type: Boolean, default: false, required: false},
      /*是否处理流程*/
      task: {type: Boolean, default: false, required: false}
    },
    data () {
      return {
        url:{
          getForm:'/actBusiness/getForm',
          addApply:'/actBusiness/add',
          editForm:'/actBusiness/editForm',
        },
        description: '',
        // form
        form: this.$form.createForm(this),
        /*表单回显数据*/
        data: {},
        btndisabled: false,
      }
    },
    created() {
      if (!this.isNew){
        //this.init();
      }
    },
    mounted(){
      this.init();
    },
    methods: {
      /*回显数据*/
      init(){
        this.btndisabled = true;
        let r = this.processData;
        console.log(r)
        getAction(this.url.getForm,{
          tableId:r.tableId,
          tableName:r.tableName,
        }).then((res)=>{
          if (res.success){
            let formData = res.result;
            formData.tableName = r.tableName;
            this.data = formData;
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(this.data,'name'))
            });
            this.btndisabled = false;

            if(formData.title === '采购订单') {
              this.$refs.purchaseOrder.show = true;
              this.$refs.purchaseOrder.initData(JSON.parse(formData.params));
            } else if(formData.title === '采购入库单') {
              this.$refs.purchaseOrderStockIn.show = true;
              this.$refs.purchaseOrderStockIn.initData(JSON.parse(formData.params));
            } else if(formData.title === '采购退货单') {
              this.$refs.purchaseOrderSalesReturn.show = true;
              this.$refs.purchaseOrderSalesReturn.initData(JSON.parse(formData.params));
            } else if(formData.title === '其他入库单' || formData.title === '其他出库单') {
              this.$refs.inOrOutOrder.show = true;
              this.$refs.inOrOutOrder.initData(JSON.parse(formData.params), formData.title);
            } else if(formData.title === '调拨') {
              this.$refs.transfersOrder.show = true;
              this.$refs.transfersOrder.initData(JSON.parse(formData.params));
            } else if(formData.title === '项目结算') {
              this.$refs.projectSettlement.show = true;
              this.$refs.projectSettlement.initData(JSON.parse(formData.params));
            } else if(formData.title === '项目收款') {
              this.$refs.projectPayment.show = true;
              this.$refs.projectPayment.initData(formData.params);
              this.$refs.projectPayment.initAccountData();
            } else if(formData.title === '客户合同') {
              this.$refs.customerContract.show = true;
              this.$refs.customerContract.initData(JSON.parse(formData.params));
            } else if(formData.title === "客户废单"){
              this.$refs.customerScrapOrder.show = true;
              this.$refs.customerScrapOrder.initData(JSON.parse(formData.params));
            }
          } else {
            this.$message.error(res.message)
          }
        })
      },
      handleSubmit (e) {
        e.preventDefault()
        this.form.validateFields((err, values) => {
          if (!err) {
            let formData = Object.assign(this.data||{}, values)
            formData.procDefId = this.processData.id;
            formData.procDeTitle = this.processData.name;
            let url = this.url.addApply;
            if (!this.isNew){
              url = this.url.editForm;
            }
            this.btndisabled = true;
            postAction(url,formData).then((res)=>{
              if (res.success){
                this.$message.success("保存成功！")
                //todo 将表单的数据传给父组件
                this.$emit('afterSubmit',formData)
              } else {
                this.$message.error(res.message)
              }
            }).finally(()=>{
              this.btndisabled = false;
            })
          }
        })
      },
      close() {
        //todo 关闭后的回调
        this.$emit('close')
      },
      /*通过审批*/
      passTask() {
        this.$emit('passTask')
      },
      /*驳回审批*/
      backTask() {
        this.$emit('backTask')
      }
    }
  }
</script>